<?php

/*
采购订单列表
*/
namespace Modules\Promanagement\Http\Repositories;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Catch\Exceptions\FailedException;
use Modules\Base\Service\BoxService;
use Modules\Promanagement\Models\Processflow;
use Modules\Promanagement\Models\Provflowsional;
use extend\services\warehouse\{warehouseService};//引入自定义类

class ProcessflowRequest
{
   
    
    /*
    数据添加方法
    */
    public static function create(array $data)
    {
   
        $Processflow = new Processflow;
        return self::createOrSave($Processflow, $data);
    }

    //数据添加方法
    public static function createOrSave(Processflow $Processflow,$data)
    {

      
        try {
            //开始起事务
         DB::beginTransaction();

            $Processflowdata = [];
            $Processflowdata['pflcode'] = $data['pflcode'];
            $Processflowdata['processname'] = $data['processname'];
            $Processflowdata['description'] = $data['description'];
     
            $Processflow->fill($Processflowdata);
            $Processflow->save();
            
            // $data['detailscheck_all'] = [
            //     [ 
            //         'processdata_id'=>'5',
            //         'process'=>1,
            //         'unitprice'=>1,
            //     ],
            //     [
            //         'processdata_id'=>'2',
            //         'process'=>1,
            //         'unitprice'=>1,
            //     ]
            // ];
            $Processflow->detailscheckAll()->createMany( $data['detailscheck_all'] );
            DB::commit();
            return ['code'=>1000,'message'=>'数据添加成功'];
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException('数据添加失败，服务器上火星了请联系技术处理');
        }
    }

    //修改
    public static function createUpdate($id,$data)
    {
       
        try {
            //开始起事务
            DB::beginTransaction();
            $Inspectionlist = Processflow::query()->updateOrCreate(['id' => $data['id']], [
                'pflcode'       => $data['pflcode']  ?? '',
                'processname'       => $data['processname']  ?? '',
                'description'       => $data['description']  ?? '',
                'modifier'       => $data['modifier']  ?? '',
            ]);
            Provflowsional::where('processflow_id',$id)->delete();
            $Inspectionlist->detailscheckAll()->createMany($data['detailscheck_all']);  
                 
            DB::commit();
            return ['code'=>1000,'message'=>'修改成功'];
        } catch (\Exception $e) {
            DB::rollBack();
            throw new FailedException('服务器飞出地球了请联系技术找回');
        }

    }


}
